package com.itheima.utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {
    private static String url;
    private static String user;
    private static String password;
    private static String driverClass;

    private JdbcUtils(){

    }
    static {
        try {
            InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("config.properties");
            Properties pro = new Properties();
            pro.load(is);
            driverClass = pro.getProperty("driverClass");
            Class.forName(driverClass);
            url = pro.getProperty("url");
            user=pro.getProperty("user");
            password=pro.getProperty("password");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getCon() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }










    /*释放资源的方法*/
    public static void close(ResultSet rs, Statement stat, Connection con){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (stat!=null){
            try {
                stat.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //利用方法重载
    public static void close(Statement stat,Connection con){
        close(null,stat,con);
    }
}
